package com.mofang.ticketing.common.log;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * service层系统日志处理
 *
 * @author xunyong
 * @date 2021/7/22 15:12
 * @copyright 2021 mofang. All rights reserved
 */
@Slf4j
public class ServiceLogHandler extends AbstractHandlerLogger {

    public ServiceLogHandler(String methName, Object response, SystemLog systemLog, Date startTime, Date endTime, Object[] methodParams,
                             Exception exception) {
        super(methName, response, systemLog, startTime, endTime, methodParams, exception);
    }

    @Override
    public void outPutLogger() throws Throwable {
        StringBuilder sb = new StringBuilder();
        //方法日志
        sb.append("\n");
        sb.append("<===================================================").append("\n");
        sb.append("[method name ]:").append(methodName).append("\n");
        if (systemLog.show_req_body()) {
            sb.append("[method params]:").append(JSONUtil.toJsonStr(methodParams)).append("\n");
        } else {
            sb.append("[method params]:").append("not show").append("\n");
        }
        sb.append("[begin time]:").append(DateUtil.format(startTime, DatePattern.NORM_DATETIME_MS_PATTERN)).append("\n");
        if (exception != null) {
            sb.append("**********************************异常*************************************");
            log.error(sb.toString(), exception);
            sb.append("===================================================>").append("\n");
            throw exception;
        } else {
            if (systemLog.show_resp_body()) {
                sb.append("[method return]:").append(JSONUtil.toJsonStr(response)).append("\n");
            } else {
                sb.append("[method return]:").append("not show").append("\n");
            }
            //处理返回参数

            sb.append("[end time]:").append(DateUtil.format(endTime, DatePattern.NORM_DATETIME_MS_PATTERN)).append("\n");
            sb.append("[consume time]:").append(DateUtil.between(startTime, endTime, DateUnit.MS)).append("ms").append("\n");
            sb.append("===================================================>").append("\n");
            log.info(sb.toString());
        }
    }
}
